// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;

using DataModel;


namespace WebLib
{
    class IndividualPage : WebPage
    {

        private static void WriteNameHeader(HtmlTextWriter writer, FamilyIndividual indi)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "name_header");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            WriteName(writer, indi.Name);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "lifespan");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(indi.LifeSpan);
            writer.RenderEndTag();

            writer.RenderEndTag();
        }



        private static void WriteEvents(HtmlTextWriter writer, FamilyIndividual indi)
        {
            BlockTable table = new BlockTable("Event", "Date", "Place", "Source");
            table.BannerText = "Events";

            foreach (FamilyEvent eve in indi.Events)
            {
                BlockTable.Row row = table.AddRow();
                row[0] = eve.Kind;
                row[1] = eve.Date;
                row[2] = eve.Place;

                if (eve.Source != null)
                {
                    row[3] = eve.Source.Title;  // TODO - Doug - 8/6/2006 - Make this a link
                }
            }

            table.Write(writer);
        }



        private static void WriteRelationships(HtmlTextWriter writer, FileNameBuilder namer, FamilyIndividual indi)
        {
            BlockTable table = new BlockTable("Type", "Name", "Source");
            table.BannerText = "Relationships";

            if (indi.Father != null)
            {
                BlockTable.Row row = table.AddRow();
                row[0] = "Father";
                row[1] = GetIndividualLink(namer, indi.Father);
                // TODO - Doug - 8/5/2006 - include the source info
            }

            if (indi.Mother != null)
            {
                BlockTable.Row row = table.AddRow();
                row[0] = "Mother";
                row[1] = GetIndividualLink(namer, indi.Mother);
                // TODO - Doug - 8/5/2006 - include the source info
            }

            // TODO - Doug - 8/5/2006 - the children should be sorted by birth date
            foreach (FamilyIndividual child in indi.Children)
            {
                BlockTable.Row row = table.AddRow();
                row[0] = "Child";
                row[1] = GetIndividualLink(namer, child);
                // TODO - Doug - 8/5/2006 - include the source info
            }

            List<FamilyIndividual> children = indi.Children;

            table.Write(writer);
        }



        private static void WriteAttributes(HtmlTextWriter writer, FamilyIndividual indi)
        {
            BlockTable table = new BlockTable("Attribute", "Value", "Source");
            table.BannerText = "Attributes";

            // TODO - Doug - 9/4/2006 - dump all the attributes into a list and sort them
            foreach (FamilyPersona persona in indi.Personas)
            {
                // TODO - Doug - 9/4/2006 - handle "empty" names
                BlockTable.Row row = table.AddRow();
                row[0] = "Name";
                row[1] = persona.Name.ToString();
                row[2] = persona.Extract.Source.Title;  // TODO - Doug - 9/4/2006 = make this into a link

                if (persona.Sex != FamilyPersona.SexEnum.Unknown)
                {
                    row = table.AddRow();
                    row[0] = "Sex";
                    row[1] = persona.Sex.ToString();
                    row[2] = persona.Extract.Source.Title;  // TODO - Doug - 9/4/2006 = make this into a link
                }
            }

            table.Write(writer);
        }


        public static void Generate(FamilyIndividual indi, FileNameBuilder namer, FamilyInfo info)
        {
            FileInfo page = namer.GetPageName(indi);

            using (HtmlTextWriter writer = new HtmlTextWriter(page.CreateText(), string.Empty))
            {
                WriteHeader(writer, indi.Name.ToString());
                WriteTitle(writer, indi.Name.ToString());
                WriteMenu(writer, namer, info);

                writer.AddAttribute(HtmlTextWriterAttribute.Id, "GeneratedContent");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                // Name and lifespan
                WriteNameHeader(writer, indi);

                // Events
                WriteEvents(writer, indi);

                // Relatives
                WriteRelationships(writer, namer, indi);

                // Attributes
                WriteAttributes(writer, indi);
                writer.RenderEndTag();
                // Pedigree?
                // TODO - Doug - 8/5/2006 - write out the pedigree?

                // Close everything off...


                writer.Close();
            }
        }

    }
}